using System;
using System.ComponentModel;
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ICP_MS.Plugin.Tune;

public class FormMinor : Form
{
	private bool isDispose = false;

	private ToolTip ttpSettings = new ToolTip();

	private IContainer components = null;

	private TurnCntlFeed tcfAux;

	private TurnCntlFeed tcfCool;

	private TurnCntlFeed tcfDilution;

	private FlowLayoutPanel flowLayoutPanel1;

	private TurnCntlFeed tcfCollision;

	private TurnCntlFeed tcfDA;

	private TurnCntlFeed tcfQE;

	private TurnCntlFeed tcfCollision2;

	private TurnCntlFeed tcfPeristic;

	public FormMinor()
	{
		InitializeComponent();
		ttpSettings.InitialDelay = 200;
		ttpSettings.AutoPopDelay = 10000;
		ttpSettings.ReshowDelay = 200;
		ttpSettings.ShowAlways = true;
		ttpSettings.IsBalloon = true;
	}

	private void InitConfigs(Platform plat)
	{
		double quadVacuum = Vacuum.vacumm.ReadVacuum[2];
		int status1 = Vacuum.vacumm.ReadVacuumStatus[2];
		try
		{
			tcfCool.Value = Convert.ToDecimal(PlasmaManager.plasma.Cool);
			tcfAux.Value = Convert.ToDecimal(PlasmaManager.plasma.Auxiliary);
			tcfDilution.Value = Convert.ToDecimal(PlasmaManager.plasma.Appendix1);
			tcfPeristic.Value = Convert.ToDecimal(PlatManager.Instrumentinjector.Perist / 10);
			tcfCollision.Value = Convert.ToDecimal(PlasmaManager.plasma.Collision);
			tcfQE.Value = Convert.ToDecimal(LensManager.x2lens.QE);
			tcfDA.Value = Convert.ToDecimal(LensManager.x2lens.DA);
			tcfCollision2.Value = Convert.ToDecimal(PlasmaManager.plasma.Collision2);
		}
		catch
		{
			Log.InsertLog("Minor", "Params exceed limit.", "", (Severity)3);
		}
	}

	private void TermConfigs()
	{
		RemoveConfigsMsg();
	}

	private void AddConfigsMsg()
	{
		tcfCool.ValueChanged += Minor_ValueChanged;
		tcfAux.ValueChanged += Minor_ValueChanged;
		tcfDilution.ValueChanged += Minor_ValueChanged;
		tcfPeristic.ValueChanged += Minor_ValueChanged;
		tcfCollision.ValueChanged += Minor_ValueChanged;
		tcfDA.ValueChanged += Minor_ValueChanged;
		tcfQE.ValueChanged += Minor_ValueChanged;
		tcfCollision2.ValueChanged += Minor_ValueChanged;
	}

	private void RemoveConfigsMsg()
	{
		tcfCool.ValueChanged -= Minor_ValueChanged;
		tcfAux.ValueChanged -= Minor_ValueChanged;
		tcfDilution.ValueChanged -= Minor_ValueChanged;
		tcfPeristic.ValueChanged -= Minor_ValueChanged;
		tcfCollision.ValueChanged -= Minor_ValueChanged;
		tcfCollision2.ValueChanged -= Minor_ValueChanged;
		tcfDA.ValueChanged -= Minor_ValueChanged;
		tcfQE.ValueChanged -= Minor_ValueChanged;
	}

	private void Minor_ValueChanged(object sender, EventArgs e)
	{
		string name = (sender as TurnCntlFeed).Name;
		double value = decimal.ToDouble((sender as TurnCntlFeed).Value);
		double quadVacuum = Vacuum.vacumm.ReadVacuum[2];
		int status1 = Vacuum.vacumm.ReadVacuumStatus[2];
		bool bright = Vacuum.Instance.QuadVacuumOK;
		if (name == tcfCool.Name)
		{
			PlasmaManager.plasma.Cool = value;
			Plasma.Instance.OpenCoolGas();
		}
		else if (name == tcfAux.Name)
		{
			PlasmaManager.plasma.Auxiliary = value;
			Plasma.Instance.OpenAuxiliaryGas();
		}
		else if (name == tcfDilution.Name)
		{
			PlasmaManager.plasma.Appendix1 = value;
			Plasma.Instance.OpenAppendixGas();
		}
		else if (name == tcfCollision.Name)
		{
			PlasmaManager.plasma.Collision = value;
			Plasma.Instance.OpenCollision();
		}
		else if (name == tcfCollision2.Name)
		{
			PlasmaManager.plasma.Collision2 = value;
			Plasma.Instance.OpenCollision2();
		}
		else if (name == tcfPeristic.Name)
		{
			PlatManager.Instrumentinjector.Perist = (int)(value * 10.0);
			Plasma.Instance.SetPeristaltic(PlatManager.Instrumentinjector.Perist);
		}
		else if (name == tcfDA.Name)
		{
			if (bright || UserManager.CurrentUser.authority > 1)
			{
				LensManager.x2lens.DA = value;
				LensManager.Instance.X2_SetDA();
			}
			else
			{
				MessageBox.Show("四极杆真空未达到！不能设置DA电压", Lang.Txt("AddFormIgnite.Warn"));
			}
		}
		else if (name == tcfQE.Name)
		{
			if (bright || UserManager.CurrentUser.authority > 1)
			{
				LensManager.x2lens.QE = value;
				LensManager.Instance.X2_SetQE();
			}
			else
			{
				MessageBox.Show("四极杆真空未达到！不能设置QE电压", Lang.Txt("AddFormIgnite.Warn"));
			}
		}
	}

	private void FormLens_Load(object sender, EventArgs e)
	{
		if (Collision.Instance.CRCType == 1)
		{
			tcfCollision2.Visible = false;
		}
		if (Collision.Instance.CRCType == 2)
		{
			tcfCollision2.Visible = true;
		}
		tcfQE.TipMax = -10.0;
		tcfQE.TipMin = -30.0;
		tcfDA.TipMin = -45.0;
		tcfDA.TipMax = -30.0;
		tcfCollision.TipMax = 2.0;
		tcfCollision.TipMin = 1.4;
		tcfCollision2.TipMax = 2.0;
		tcfCollision2.TipMin = 1.4;
		InitConfigs(PlatManager.plat);
		AddConfigsMsg();
		Task.Factory.StartNew(delegate
		{
			while (!isDispose)
			{
				try
				{
					Invoke((MethodInvoker)delegate
					{
						tcfCool.FeedBack = PlasmaManager.plasma.ReadCool;
						tcfAux.FeedBack = PlasmaManager.plasma.ReadAuxiliary;
						tcfDilution.FeedBack = PlasmaManager.plasma.ReadAppendix1;
						tcfCollision.FeedBack = PlasmaManager.plasma.ReadCollision;
						tcfCollision2.FeedBack = PlasmaManager.plasma.ReadCollision2;
						tcfPeristic.FeedBack = PlatManager.injector.ReadPerist / 10;
						tcfDA.FeedBack = LensManager.x2lens.ReadDA;
						tcfQE.FeedBack = LensManager.x2lens.ReadQE;
						tcfDA.ImageFeed = LensManager.x2lens.StatusDA;
						tcfQE.ImageFeed = LensManager.x2lens.StatusQE;
					});
				}
				catch
				{
				}
				Thread.Sleep(1000);
			}
		});
		ApplyResources();
	}

	private void Tcf_MouseEnter(object sender, EventArgs e)
	{
		Control ctrl = (Control)sender;
		string tipOverwrite = " ";
		switch (ctrl.Name)
		{
		case "tcfQE":
			tipOverwrite = "建议四级前置：-30~-10";
			break;
		case "tcfLens3":
			tipOverwrite = "建议透镜3：-200~-150";
			break;
		case "tcfLens2":
			tipOverwrite = "建议透镜2：-90~-70";
			break;
		case "tcfDA":
			tipOverwrite = "建议偏转辅助：-45~-30";
			break;
		case "tcfD2":
			tipOverwrite = "建议偏转电压：-45~-25";
			break;
		}
		ttpSettings.SetToolTip(ctrl, tipOverwrite);
	}

	private void ApplyResources()
	{
		Text = Lang.Txt("FormMinor.Title.Text");
		tcfAux.Label = Lang.Txt("FormMinor.Auxilury.Text");
		tcfCool.Label = Lang.Txt("FormMinor.CoolGas.Text");
		tcfCollision.Label = Lang.Txt("FormMajor.Collision.Text");
		tcfCollision2.Label = Lang.Txt("FormMajor.Collision2.Text");
		tcfDA.Label = Lang.Txt("FormMinor.DA.Text");
		tcfQE.Label = Lang.Txt("FormMinor.QE.Text");
		tcfDilution.Label = Lang.Txt("ADDTune.FormMinor.DiluentGasFlow");
		tcfPeristic.Label = Lang.Txt("ADDTune.FormMinor.PeristalticPump");
	}

	private void FormLens_FormClosing(object sender, FormClosingEventArgs e)
	{
		isDispose = true;
		TermConfigs();
	}

	public void ConfigsChanged(object sender, EventArgs e)
	{
		RemoveConfigsMsg();
		InitConfigs((Platform)sender);
		AddConfigsMsg();
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
		this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel();
		this.tcfDA = new ICP_MS.Plugin.Tune.TurnCntlFeed();
		this.tcfQE = new ICP_MS.Plugin.Tune.TurnCntlFeed();
		this.tcfCool = new ICP_MS.Plugin.Tune.TurnCntlFeed();
		this.tcfAux = new ICP_MS.Plugin.Tune.TurnCntlFeed();
		this.tcfDilution = new ICP_MS.Plugin.Tune.TurnCntlFeed();
		this.tcfCollision = new ICP_MS.Plugin.Tune.TurnCntlFeed();
		this.tcfCollision2 = new ICP_MS.Plugin.Tune.TurnCntlFeed();
		this.tcfPeristic = new ICP_MS.Plugin.Tune.TurnCntlFeed();
		this.flowLayoutPanel1.SuspendLayout();
		base.SuspendLayout();
		this.flowLayoutPanel1.AutoScroll = true;
		this.flowLayoutPanel1.Controls.Add(this.tcfDA);
		this.flowLayoutPanel1.Controls.Add(this.tcfQE);
		this.flowLayoutPanel1.Controls.Add(this.tcfCool);
		this.flowLayoutPanel1.Controls.Add(this.tcfAux);
		this.flowLayoutPanel1.Controls.Add(this.tcfDilution);
		this.flowLayoutPanel1.Controls.Add(this.tcfCollision);
		this.flowLayoutPanel1.Controls.Add(this.tcfCollision2);
		this.flowLayoutPanel1.Controls.Add(this.tcfPeristic);
		this.flowLayoutPanel1.Location = new System.Drawing.Point(0, 0);
		this.flowLayoutPanel1.Margin = new System.Windows.Forms.Padding(4);
		this.flowLayoutPanel1.Name = "flowLayoutPanel1";
		this.flowLayoutPanel1.Size = new System.Drawing.Size(316, 1153);
		this.flowLayoutPanel1.TabIndex = 43;
		this.tcfDA.BackColor = System.Drawing.SystemColors.GradientInactiveCaption;
		this.tcfDA.C = 0.0;
		this.tcfDA.CodeId = 0u;
		this.tcfDA.Coe = new decimal(new int[4] { 1, 0, 0, 0 });
		this.tcfDA.DecimalPlaces = 2;
		this.tcfDA.Defalut = 0.0;
		this.tcfDA.DestId = 0u;
		this.tcfDA.FeedDecimal = 2;
		this.tcfDA.Increment = new decimal(new int[4] { 1, 0, 0, 0 });
		this.tcfDA.K = 0.0;
		this.tcfDA.Label = "DA";
		this.tcfDA.Location = new System.Drawing.Point(7, 6);
		this.tcfDA.Margin = new System.Windows.Forms.Padding(7, 6, 7, 6);
		this.tcfDA.MaxValue = new decimal(new int[4]);
		this.tcfDA.MinValue = new decimal(new int[4] { 200, 0, 0, -2147483648 });
		this.tcfDA.Name = "tcfDA";
		this.tcfDA.Size = new System.Drawing.Size(308, 65);
		this.tcfDA.TabIndex = 45;
		this.tcfDA.TipMax = double.NaN;
		this.tcfDA.TipMin = double.NaN;
		this.tcfDA.Value = new decimal(new int[4]);
		this.tcfQE.BackColor = System.Drawing.SystemColors.GradientInactiveCaption;
		this.tcfQE.C = 0.0;
		this.tcfQE.CodeId = 0u;
		this.tcfQE.Coe = new decimal(new int[4] { 1, 0, 0, 0 });
		this.tcfQE.DecimalPlaces = 2;
		this.tcfQE.Defalut = 0.0;
		this.tcfQE.DestId = 0u;
		this.tcfQE.FeedDecimal = 2;
		this.tcfQE.Increment = new decimal(new int[4] { 1, 0, 0, 0 });
		this.tcfQE.K = 0.0;
		this.tcfQE.Label = "QE";
		this.tcfQE.Location = new System.Drawing.Point(7, 83);
		this.tcfQE.Margin = new System.Windows.Forms.Padding(7, 6, 7, 6);
		this.tcfQE.MaxValue = new decimal(new int[4]);
		this.tcfQE.MinValue = new decimal(new int[4] { 200, 0, 0, -2147483648 });
		this.tcfQE.Name = "tcfQE";
		this.tcfQE.Size = new System.Drawing.Size(308, 65);
		this.tcfQE.TabIndex = 46;
		this.tcfQE.TipMax = double.NaN;
		this.tcfQE.TipMin = double.NaN;
		this.tcfQE.Value = new decimal(new int[4]);
		this.tcfCool.BackColor = System.Drawing.SystemColors.GradientInactiveCaption;
		this.tcfCool.C = 0.0;
		this.tcfCool.CodeId = 0u;
		this.tcfCool.Coe = new decimal(new int[4] { 1, 0, 0, 0 });
		this.tcfCool.DecimalPlaces = 2;
		this.tcfCool.Defalut = 0.0;
		this.tcfCool.DestId = 0u;
		this.tcfCool.FeedDecimal = 2;
		this.tcfCool.Increment = new decimal(new int[4] { 1, 0, 0, 65536 });
		this.tcfCool.K = 0.0;
		this.tcfCool.Label = "冷却气流量(L/min)";
		this.tcfCool.Location = new System.Drawing.Point(7, 160);
		this.tcfCool.Margin = new System.Windows.Forms.Padding(7, 6, 7, 6);
		this.tcfCool.MaxValue = new decimal(new int[4] { 20, 0, 0, 0 });
		this.tcfCool.MinValue = new decimal(new int[4]);
		this.tcfCool.Name = "tcfCool";
		this.tcfCool.Size = new System.Drawing.Size(308, 65);
		this.tcfCool.TabIndex = 40;
		this.tcfCool.TipMax = double.NaN;
		this.tcfCool.TipMin = double.NaN;
		this.tcfCool.Value = new decimal(new int[4]);
		this.tcfAux.BackColor = System.Drawing.SystemColors.GradientInactiveCaption;
		this.tcfAux.C = 0.0;
		this.tcfAux.CodeId = 0u;
		this.tcfAux.Coe = new decimal(new int[4] { 1, 0, 0, 0 });
		this.tcfAux.DecimalPlaces = 2;
		this.tcfAux.Defalut = 0.0;
		this.tcfAux.DestId = 0u;
		this.tcfAux.FeedDecimal = 2;
		this.tcfAux.Increment = new decimal(new int[4] { 1, 0, 0, 131072 });
		this.tcfAux.K = 0.0;
		this.tcfAux.Label = "辅助气流量(L/min)";
		this.tcfAux.Location = new System.Drawing.Point(7, 237);
		this.tcfAux.Margin = new System.Windows.Forms.Padding(7, 6, 7, 6);
		this.tcfAux.MaxValue = new decimal(new int[4] { 2, 0, 0, 0 });
		this.tcfAux.MinValue = new decimal(new int[4]);
		this.tcfAux.Name = "tcfAux";
		this.tcfAux.Size = new System.Drawing.Size(308, 65);
		this.tcfAux.TabIndex = 41;
		this.tcfAux.TipMax = double.NaN;
		this.tcfAux.TipMin = double.NaN;
		this.tcfAux.Value = new decimal(new int[4]);
		this.tcfDilution.BackColor = System.Drawing.SystemColors.GradientInactiveCaption;
		this.tcfDilution.C = 0.0;
		this.tcfDilution.CodeId = 0u;
		this.tcfDilution.Coe = new decimal(new int[4] { 1, 0, 0, 0 });
		this.tcfDilution.DecimalPlaces = 2;
		this.tcfDilution.Defalut = 0.0;
		this.tcfDilution.DestId = 0u;
		this.tcfDilution.FeedDecimal = 2;
		this.tcfDilution.Increment = new decimal(new int[4] { 1, 0, 0, 131072 });
		this.tcfDilution.K = 0.0;
		this.tcfDilution.Label = "稀释气流量(L/min)";
		this.tcfDilution.Location = new System.Drawing.Point(7, 314);
		this.tcfDilution.Margin = new System.Windows.Forms.Padding(7, 6, 7, 6);
		this.tcfDilution.MaxValue = new decimal(new int[4] { 2, 0, 0, 0 });
		this.tcfDilution.MinValue = new decimal(new int[4]);
		this.tcfDilution.Name = "tcfDilution";
		this.tcfDilution.Size = new System.Drawing.Size(308, 65);
		this.tcfDilution.TabIndex = 42;
		this.tcfDilution.TipMax = double.NaN;
		this.tcfDilution.TipMin = double.NaN;
		this.tcfDilution.Value = new decimal(new int[4]);
		this.tcfCollision.BackColor = System.Drawing.SystemColors.GradientInactiveCaption;
		this.tcfCollision.C = 0.0;
		this.tcfCollision.CodeId = 0u;
		this.tcfCollision.Coe = new decimal(new int[4] { 1, 0, 0, 0 });
		this.tcfCollision.DecimalPlaces = 2;
		this.tcfCollision.Defalut = 0.0;
		this.tcfCollision.DestId = 0u;
		this.tcfCollision.FeedDecimal = 2;
		this.tcfCollision.Increment = new decimal(new int[4] { 1, 0, 0, 131072 });
		this.tcfCollision.K = 0.0;
		this.tcfCollision.Label = "碰撞反应池1(mL/min)";
		this.tcfCollision.Location = new System.Drawing.Point(7, 391);
		this.tcfCollision.Margin = new System.Windows.Forms.Padding(7, 6, 7, 6);
		this.tcfCollision.MaxValue = new decimal(new int[4] { 10, 0, 0, 0 });
		this.tcfCollision.MinValue = new decimal(new int[4]);
		this.tcfCollision.Name = "tcfCollision";
		this.tcfCollision.Size = new System.Drawing.Size(308, 65);
		this.tcfCollision.TabIndex = 43;
		this.tcfCollision.TipMax = double.NaN;
		this.tcfCollision.TipMin = double.NaN;
		this.tcfCollision.Value = new decimal(new int[4]);
		this.tcfCollision2.BackColor = System.Drawing.SystemColors.GradientInactiveCaption;
		this.tcfCollision2.C = 0.0;
		this.tcfCollision2.CodeId = 0u;
		this.tcfCollision2.Coe = new decimal(new int[4] { 1, 0, 0, 0 });
		this.tcfCollision2.DecimalPlaces = 2;
		this.tcfCollision2.Defalut = 0.0;
		this.tcfCollision2.DestId = 0u;
		this.tcfCollision2.FeedDecimal = 2;
		this.tcfCollision2.Increment = new decimal(new int[4] { 1, 0, 0, 131072 });
		this.tcfCollision2.K = 0.0;
		this.tcfCollision2.Label = "碰撞反应池2(mL/min)";
		this.tcfCollision2.Location = new System.Drawing.Point(7, 468);
		this.tcfCollision2.Margin = new System.Windows.Forms.Padding(7, 6, 7, 6);
		this.tcfCollision2.MaxValue = new decimal(new int[4] { 10, 0, 0, 0 });
		this.tcfCollision2.MinValue = new decimal(new int[4]);
		this.tcfCollision2.Name = "tcfCollision2";
		this.tcfCollision2.Size = new System.Drawing.Size(308, 65);
		this.tcfCollision2.TabIndex = 47;
		this.tcfCollision2.TipMax = double.NaN;
		this.tcfCollision2.TipMin = double.NaN;
		this.tcfCollision2.Value = new decimal(new int[4]);
		this.tcfCollision2.Visible = false;
		this.tcfPeristic.BackColor = System.Drawing.SystemColors.GradientInactiveCaption;
		this.tcfPeristic.C = 0.0;
		this.tcfPeristic.CodeId = 0u;
		this.tcfPeristic.Coe = new decimal(new int[4] { 1, 0, 0, 0 });
		this.tcfPeristic.DecimalPlaces = 0;
		this.tcfPeristic.Defalut = 0.0;
		this.tcfPeristic.DestId = 0u;
		this.tcfPeristic.FeedDecimal = 2;
		this.tcfPeristic.Increment = new decimal(new int[4] { 5, 0, 0, 0 });
		this.tcfPeristic.K = 0.0;
		this.tcfPeristic.Label = "蠕动泵(r/min)";
		this.tcfPeristic.Location = new System.Drawing.Point(7, 545);
		this.tcfPeristic.Margin = new System.Windows.Forms.Padding(7, 6, 7, 6);
		this.tcfPeristic.MaxValue = new decimal(new int[4] { 80, 0, 0, 0 });
		this.tcfPeristic.MinValue = new decimal(new int[4]);
		this.tcfPeristic.Name = "tcfPeristic";
		this.tcfPeristic.Size = new System.Drawing.Size(308, 65);
		this.tcfPeristic.TabIndex = 48;
		this.tcfPeristic.TipMax = double.NaN;
		this.tcfPeristic.TipMin = double.NaN;
		this.tcfPeristic.Value = new decimal(new int[4]);
		base.AutoScaleDimensions = new System.Drawing.SizeF(8f, 15f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		this.AutoSize = true;
		base.ClientSize = new System.Drawing.Size(316, 1055);
		base.Controls.Add(this.flowLayoutPanel1);
		base.Margin = new System.Windows.Forms.Padding(5);
		base.Name = "FormMinor";
		base.ShowIcon = false;
		this.Text = "FormX2Lens";
		base.FormClosing += new System.Windows.Forms.FormClosingEventHandler(FormLens_FormClosing);
		base.Load += new System.EventHandler(FormLens_Load);
		this.flowLayoutPanel1.ResumeLayout(false);
		base.ResumeLayout(false);
	}
}
